﻿<?php
if(!defined('IN_PHPSCUP'))exit('hacking'); 

class AdplaceApp extends BaseApp
{
    var $adtype=array(
					"word"=>"文字广告",
					"pic"=>"图片广告",
					"swf"=>"FLASH广告",
					"code"=>"代码广告",
					"duilian"=>"对联广告"
					);
    var $type = 'adplace';
	
	
    function index()
    {
				
		$TB_links = &$this->_model('adplace'); 		
		$data = $TB_links->show();				
        $this->assign('data',$data);
        $this->assign('datype',$this->adtype);	    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));			
        $this->assign('site_url',SITE_URL);		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');		
        $this->display('adplace_list.view.php');
    }

    function add()
    {
        $this->assign('keywords',"AD_".rand(1,9999));		
        $this->assign('datype',$this->adtype);		    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('adplace_add.view.php');
    }
    function addpost()
    {

	$data = $_POST['data'];
    $postdb = array();
	if($data['type']=='word'){
		$cdb['word']=$data['word'];	
		$cdb['linkurl']=$data['wordlinkurl'];
		$cdb['wordtarget']=$data['wordtarget'];
	}elseif($data['type']=='pic'){	
		$cdb['picurl']=$data['picurl'];
		$cdb['linkurl']=$data['piclinkurl'];	
		$cdb['width']=$data['picwidth'];
		$cdb['height']=$data['picheight'];
		$cdb['pictarget']=$data['pictarget'];
	}elseif($data['type']=='swf'){
		$cdb['flashurl']=$data['flashurl'];	
		$cdb['width']=$data['swfwidth'];
		$cdb['height']=$data['swfheight'];
	}elseif($data['type']=='duilian'){
		$cdb['l_src']=$data['l_src'];
		$cdb['l_link']=$data['l_link'];
		$cdb['l_width']=$data['l_width'];
		$cdb['l_height']=$data['l_height'];
		$cdb['r_src']=$data['r_src'];
		$cdb['r_link']=$data['r_link'];
		$cdb['r_width']=$data['r_width'];
		$cdb['r_height']=$data['r_height'];
	}else if ($data['type']=='code') {
	    $cdb['code']=stripslashes($data['code']);
	}	
	$postdb['adcode']=addslashes(serialize($cdb));
	
	if($data['ifsale'])
	{
		$postdb['begintime']=$postdb['endtime']=0;
	}
	
	$data['begintime']&& $postdb['begintime']=strtotime($data['begintime']);
	$data['endtime']&& $postdb['endtime']=strtotime($data['endtime']);
    $postdb['name'] = $data['name'];
    $postdb['demourl'] = $data['demourl'];	
    $postdb['keywords'] = $data['keywords'];	
    $postdb['type'] = $data['type'];
    $postdb['ifsale'] = $data['ifsale'];
    $postdb['autoyz'] = $data['autoyz'];
    $postdb['moneycard'] = $data['moneycard'];	
		 
		$TB_links = &$this->_model('adplace'); 		
		$TB_links->post($postdb);
	$this->make_ad_cache();		
		$this->index();

    }	
	
    function edit()
    {
		
	    $id = $_GET['id'];	

		$TB_links = &$this->_model('adplace');
		
		$picdata = $TB_links->oneshow($id);
        $data = unserialize($picdata[adcode]);
	    if($picdata['type']=='word'){
		   $data['wordlinkurl']=$data['linkurl'];
	    }elseif($picdata['type']=='pic'){
		   $data['piclinkurl']=$data['linkurl'];	
		   $data['picwidth']=$data['width'];
		   $data['picheight']=$data['height'];
	    }elseif($picdata['type']=='swf'){
		   $data['swfwidth']=$data['width'];
		   $data['swfheight']=$data['height'];
	    }	
        $picdata = $picdata + $data ;		
        $this->assign('data',$picdata);		    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('adplace_edit.view.php');
    }





    function editpost()
    {

	     $data = $_POST['data'];
		 $id = $_POST['id'];
    $postdb = array();
	if($data['type']=='word'){
		$cdb['word']=$data['word'];	
		$cdb['linkurl']=$data['wordlinkurl'];
		$cdb['wordtarget']=$data['wordtarget'];
	}elseif($data['type']=='pic'){	
		$cdb['picurl']=$data['picurl'];
		$cdb['linkurl']=$data['piclinkurl'];	
		$cdb['width']=$data['picwidth'];
		$cdb['height']=$data['picheight'];
		$cdb['pictarget']=$data['pictarget'];
	}elseif($data['type']=='swf'){
		$cdb['flashurl']=$data['flashurl'];	
		$cdb['width']=$data['swfwidth'];
		$cdb['height']=$data['swfheight'];
	}elseif($data['type']=='duilian'){
		$cdb['l_src']=$data['l_src'];
		$cdb['l_link']=$data['l_link'];
		$cdb['l_width']=$data['l_width'];
		$cdb['l_height']=$data['l_height'];
		$cdb['r_src']=$data['r_src'];
		$cdb['r_link']=$data['r_link'];
		$cdb['r_width']=$data['r_width'];
		$cdb['r_height']=$data['r_height'];
	}else if ($data['type']=='code') {
	    $cdb['code']=stripslashes($data['code']);
	}	
	$postdb['adcode']=addslashes(serialize($cdb));
	
	if($data['ifsale'])
	{
		$postdb['begintime']=$postdb['endtime']=0;
	}
	
	$data['begintime']&& $postdb['begintime']=strtotime($data['begintime']);
	$data['endtime']&& $postdb['endtime']=strtotime($data['endtime']);
    $postdb['name'] = $data['name'];
    $postdb['demourl'] = $data['demourl'];	
    $postdb['keywords'] = $data['keywords'];	
    $postdb['type'] = $data['type'];
    $postdb['ifsale'] = $data['ifsale'];
    $postdb['autoyz'] = $data['autoyz'];
    $postdb['moneycard'] = $data['moneycard'];	
			 
		 
		 
		$TB_links = &$this->_model('adplace'); 	
		$TB_links->editaction($id,$postdb);
		 
	    $this->make_ad_cache();	
		 
		$this->index();

    }
	
    function del()
    {

	    $id = $_GET['id'];
        $db = & get_db();
	    $pre = DB_PREFIX;		 
	    $db->query("DELETE FROM `{$pre}ad_place` WHERE id='$id'");
	    $db->query("DELETE FROM `{$pre}ad_user` WHERE id='$id'");
	    $this->make_ad_cache();		 
		$this->index();	  

    }

    function kindeditor()
    {
    $w = intval($_GET['w']);
    include(ROOT_PATH."/data/ewebeditor/ewebeditor.lib.php");	
	$kindeditor = new Ewebeditor(array('imageUploadCgi'=>SITE_URL.'/admin/index.php?app='.$this->type.'&act=kindeditorupfile&w='.$w));
	$kindeditor->show();
	
    }
	
    function kindeditorupfile()
    {
    $w = intval($_GET['w']);
    include(ROOT_PATH."/data/ewebeditor/ewebeditor.lib.php");	
	$kindeditor = new Ewebeditor(array('imageUploadCgi'=>SITE_URL.'/admin/index.php?app='.$this->type.'&act=kindeditorupfile&w='.$w));
	$kindeditor->upfile($this->type.'/'.$w);	
    }
		
    function upfile()
    {
	 if(!isset($_POST["action"]))
	 {
	 $_POST["action"]='';
	 }
	 if(!isset($_GET['id']))
	 {
	 $_GET['id']='';
	 }
	 if(!isset($_GET["edit"]))
	 {
	 $_GET["edit"]='';
	 }	 
	 if(!isset($_GET["picurl"]))
	 {
	 $_GET["picurl"]='';
	 }	
	 if(!isset($_GET['jsfun']))
	 {
	 $_GET['jsfun']='';
	 }		 
	 if(!isset($_GET['putid']))
	 {
	 $_GET['putid']='';
	 }		 
	 if(!isset($_GET['uppic']))
	 {
	 $_GET['uppic']='';
	 }	
		$tf = $_GET['tf'];
        if(empty($tf))$tf = $_POST['tf'];	 	 	
		$action = $_POST["action"];		
		$id = intval($_GET['id']);		
		$jsfun = $_GET['jsfun'];
		$uppic = $_GET['putid'];
		if(!empty($_GET['uppic']))
		{
		$uppic = $_GET['uppic'];		
		}	
		$edit = $_GET["edit"];
		$picurl = $_GET["picurl"];

        $this->assign('jsfun', $jsfun);		


        $this->assign('uppic', $uppic);		
	
				

        $this->assign('edit', $edit);		


        $this->assign('picurl', $picurl);

        $this->assign('tf', $tf);
						
		if($action == 'post')
		{
			 if(!isset($_POST['jsfun']))
	 {
	 $_POST['jsfun']='';
	 }	
	 if(!isset($_POST['uppic']))
	 {
	 $_POST['uppic']='';
	 }		 
	 if(!isset($_POST["edit"]))
	 {
	 $_POST["edit"]='';
	 }		 
	 if(!isset($_POST["picurl"]))
	 {
	 $_POST["picurl"]='';
	 }	
	 if(!isset($_POST['id']))
	 {
	 $_POST['id']='';
	 }		 
		       $jsfun = $_POST['jsfun'];
		       $uppic = $_POST['uppic'];		
		       $edit = $_POST["edit"];
		       $picurl = $_POST["picurl"];
		 	   $id || $id = intval($_POST['id']);		
		       $filedname = trim(str_replace(array("UP_"),array(""),$uppic)); 
       
		       include(ROOT_PATH.'/lib/upfile.lib.php');	  
	           $upfile = new Upfile($this->type.'/'.$id); 	  
	           $updata = $upfile->_batch_upfile($this->type);		 
		 		 
		       $newfile = $updata[1]['path'];
	       
			   if($edit=='editp' && $picurl)
			   {

			          $TB_new = &$this->_model('content');			    		 		 		
		              $TB_new->editpicurl($id,array("$filedname"=>$newfile),$this->table_id);
		              $pic_dir= ROOT_PATH . '/' . UPFILE_PATH . '/' . $picurl;
			          if(!is_dir($pic_dir)&&file_exists($pic_dir))
		              {
		                del_file($pic_dir);	
		              }					  
				   
			   }

			    if($edit=='edit' && $picurl)
		       {
		              $pic_dir= ROOT_PATH . '/' . UPFILE_PATH . '/' . $picurl;
			          if(!is_dir($pic_dir)&&file_exists($pic_dir))
		              {
		                del_file($pic_dir);	
		              }				 
		       }
		
		      $pic_url = SITE_URL . '/' . UPFILE_PATH . '/' .$newfile;

              if($tf=='pic')
			  {
	           echo "<img src='{$pic_url}' id='upsrc' width='100px' height='100px'><br/><A HREF='?app=$this->type&act=upfile&edit=$edit&tf=$tf&picurl=$newfile&id=$id&jsfun=$jsfun&uppic=$uppic'>修改图片</A>";			  
			  }else
			  {
	           echo "<A HREF='?app=$this->type&act=upfile&edit=$edit&tf=$tf&picurl=$newfile&id=$id&jsfun=$jsfun&uppic=$uppic'>修改文件</A>";			  
			  }

	    echo "<script>
				if(self==top){
					window.opener.$jsfun('$newfile','$uppic');
					window.self.close();
				}else{
					window.parent.$jsfun('$newfile','$uppic');
				}
		     </script>";
				
	    exit;		
		
		}
        $this->assign('id', $id);
	    $this->assign('app', $this->type);		    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', SITE_TITLE);
        $this->assign('page_description', SITE_DESCRIPTION);
        $this->assign('page_keywords', SITE_KEYWORDS);		
        $this->assign('site_url',SITE_URL);		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');		
        $this->display('upfile.view.php');
    }

	function picdir($string) {
		$root_dir = (UPFILE_PATH == '') ? (SITE_URL . '/upfiles') : (SITE_URL . '/' . UPFILE_PATH);
		return $root_dir . '/' . $string;
	}
	
    function make_ad_cache(){
    $db = & get_db();
	$pre = DB_PREFIX;
	$wwwurl = SITE_URL;
	$timestamp = time();
	$show.="<?php\r\n";
	$query = $db->query("SELECT * FROM `{$pre}ad_place` WHERE isclose=0");
	while($rs = $db->fetch_array($query)){
		unset($_r);
		if($rs[ifsale]&&$_r=$db->get_one("SELECT u_code,u_id FROM {$pre}ad_user WHERE u_endtime>'$timestamp' AND id='$rs[id]'")){
			@extract(unserialize($_r[u_code]));
		}else{
			@extract(unserialize($rs[adcode]));
		}
		
		if($rs[type]=='word'){
			$url="$wwwurl/?app=ad&act=jump&id=$rs[id]&u_id=$_r[u_id]&url=".base64_encode($linkurl);
			$code="<SCRIPT LANGUAGE='JavaScript' src='$wwwurl/?app=ad&act=js&ad_id=$rs[keywords]'></SCRIPT>";
			$_code="<a href='$url' target='$wordtarget'>$word</a>";
		}elseif($rs[type]=='pic'){
			$url="$wwwurl/?app=ad&act=jump&id=$rs[id]&u_id=$_r[u_id]&url=".base64_encode($linkurl);
			$picurl=$this->picdir($picurl);
			$code="<SCRIPT LANGUAGE='JavaScript' src='$wwwurl/?app=ad&act=js&ad_id=$rs[keywords]'></SCRIPT>";
			$_code="<a href='$url' target='$pictarget'><img width='$width' height='$height' src='$picurl' border=0></a>";
		}elseif($rs[type]=='swf'){
			$flashurl=$this->picdir($flashurl);
			$code="<SCRIPT LANGUAGE='JavaScript' src='$wwwurl/?app=ad&act=js&ad_id=$rs[keywords]'></SCRIPT>";
			$_code="<object type='application/x-shockwave-flash' data='$flashurl' width='$width' height='$height' wmode='transparent'><param name='movie' value='$flashurl' /><param name='wmode' value='transparent' /></object>";
		}elseif($rs[type]=='duilian'){
			$lcode=$rcode='';
			if($l_src){
				$l_src=$this->picdir($l_src);
				if(eregi("swf$",$l_src)){
					$L_Types="flash";
				}else{
					$l_link="$wwwurl/?app=ad&act=jump&id=$rs[id]&url=".base64_encode($l_link);
					$L_Types="photo";
				}
			}
			if($r_src){
				$r_src=$this->picdir($r_src);
				if(eregi("swf$",$r_src)){
					$R_Types="flash";
				}else{
					$r_link="$wwwurl/?app=ad&act=jump&id=$rs[id]&url=".base64_encode($r_link);
					$R_Types="photo";
				}
			}
			$code="<SCRIPT LANGUAGE='JavaScript' src='$wwwurl/?app=ad&act=js&ad_id=$rs[keywords]'></SCRIPT>";
			$_code="<script language=\"javascript\" type=\"text/javascript\">
   var adLeftSrc = \"$l_src\"   //图片地址
   var adLeftFlash = \"$L_Types\"
   var adLeftHref = \"$l_link\"
   var adLeftWidth = '$l_width'
   var adLeftHeight = '$l_height'
   var adRightSrc = \"$r_src\"//图片地址
   var adRightFlash = \"$R_Types\"
   var adRightHref = \"$r_link\"
   var adRightWidth = '$r_width'
   var adRightHeight = '$r_height'
   var marginTop = 300 //在这里更改距离浏览器底端的高度
   var marginLeft = 5//在这里更改距离浏览器右端的高度
   var navUserAgent = navigator.userAgent
   function load(){
    judge();
    move();
   }
   function move() {
    judge();
    setTimeout(\"move();\",80)
   }
   function judge(){
    if (navUserAgent.indexOf(\"Firefox\") >= 0 || navUserAgent.indexOf(\"Opera\") >= 0) {
     if (adLeftSrc != \"\") {document.getElementById(\"adLeftFloat\").style.top = (document.body.scrollTop?document.body.scrollTop:document.documentElement.scrollTop) + ((document.body.clientHeight > document.documentElement.clientHeight)?document.documentElement.clientHeight:document.body.clientHeight) - adLeftHeight - marginTop + 'px';}
     if (adRightSrc != \"\") {
      document.getElementById(\"adRightFloat\").style.top = (document.body.scrollTop?document.body.scrollTop:document.documentElement.scrollTop) + ((document.body.clientHeight > document.documentElement.clientHeight)?document.documentElement.clientHeight:document.body.clientHeight) - adRightHeight - marginTop + 'px';
      document.getElementById(\"adRightFloat\").style.left = ((document.body.clientWidth > document.documentElement.clientWidth)?document.body.clientWidth:document.documentElement.clientWidth) - adRightWidth - marginLeft + 'px';
     } 
    }
    else{
     if (adLeftSrc != \"\") {document.getElementById(\"adLeftFloat\").style.top = (document.body.scrollTop?document.body.scrollTop:document.documentElement.scrollTop) + ((document.documentElement.clientHeight == 0)?document.body.clientHeight:document.documentElement.clientHeight) - adLeftHeight - marginTop + 'px';}
     if (adRightSrc != \"\") {
      document.getElementById(\"adRightFloat\").style.top = (document.body.scrollTop?document.body.scrollTop:document.documentElement.scrollTop) + ((document.documentElement.clientHeight == 0)?document.body.clientHeight:document.documentElement.clientHeight) - adRightHeight - marginTop + 'px';
      document.getElementById(\"adRightFloat\").style.left = ((document.documentElement.clientWidth == 0)?document.body.clientWidth:document.documentElement.clientWidth) - adRightWidth - marginLeft + 'px';
     }
    }
    if (adLeftSrc != \"\") {document.getElementById(\"adLeftFloat\").style.left = marginLeft + 'px';}
   }
    
if (adLeftSrc != \"\") {
	if (adLeftFlash == \"flash\") {
		document.write(\"<div id=\\\"adLeftFloat\\\" style=\\\"position: absolute;width:\" + adLeftWidth + \";height:100px; font-size:12px;\\\"><div id='left_top'><img src=\\\"$webdb[www_url]/images/default/close.gif\\\" width=\\\"12\\\" border=\\\"0\\\" height=\\\"12\\\" onMousedown=\\\"javascript:close_ad('left_top');void(0);\\\";>\");
    
		document.write(\"<div id=\\\"Float\\\" style=\\\"width:\" + adLeftWidth + \";\\\"><embed src=\\\"\" + adLeftSrc + \"\\\" quality=\\\"high\\\"  width=\\\"\" + adLeftWidth + \"\\\" height=\\\"\" + adLeftHeight + \"\\\" type=\\\"application/x-shockwave-flash\\\"></embed></div></div>\");
		document.write(\"</div>\");
    }else{
		document.write(\"<div id=\\\"adLeftFloat\\\" style=\\\"position: absolute;width:\" + adLeftWidth + \";height:100px; font-size:12px;\\\"><img src=\\\"$webdb[www_url]/images/default/close.gif\\\" width=\\\"12\\\" border=\\\"0\\\" height=\\\"12\\\" onMousedown=\\\"javascript:close_float_left();void(0);\\\";>\");
    
		document.write(\"<div id=\\\"Float\\\" style=\\\"width:\" + adLeftWidth + \";\\\"><a href=\\\"\" + adLeftHref +\"\\\" target=_blank><img src=\\\"\" + adLeftSrc + \"\\\"  width=\\\"\" + adLeftWidth + \"\\\" height=\\\"\" + adLeftHeight + \"\\\"  border=\\\"0\\\" \></a></div>\");
		document.write(\"</div>\");
    }
}
  
if (adRightSrc != \"\") {
	if (adRightFlash == \"flash\") {
		document.write(\"<div id=\\\"adRightFloat\\\" style=\\\"position: absolute;width:\" + adRightWidth + \";height:100px; font-size:12px;text-align:right;\\\"><div id='right_top'><img src=\\\"$webdb[www_url]/images/default/close.gif\\\" width=\\\"12\\\" border=\\\"0\\\" height=\\\"12\\\" onMousedown=\\\"javascript:close_ad('right_top');void(0);\\\";>\");
    
		document.write(\"<div id=\\\"Float\\\" style=\\\"width:\" + adRightWidth + \";\\\"><embed src=\\\"\" + adRightSrc + \"\\\" quality=\\\"high\\\"  width=\\\"\" + adLeftWidth + \"\\\" height=\\\"\" + adRightHeight + \"\\\" type=\\\"application/x-shockwave-flash\\\"></embed></div></div>\");
		document.write(\"</div>\");
    }else{
		document.write(\"<div id=\\\"adRightFloat\\\" style=\\\"position: absolute;width:\" + adRightWidth + \";height:100px; font-size:12px;text-align:right;\\\"><img src=\\\"$webdb[www_url]/images/default/close.gif\\\" width=\\\"12\\\" border=\\\"0\\\" height=\\\"12\\\" onMousedown=\\\"javascript:close_float_right();void(0);\\\";>\");
    
		document.write(\"<div id=\\\"Float\\\" style=\\\"width:\" + adRightWidth + \";\\\"><a href=\\\"\" + adRightHref +\"\\\" target=_blank><img src=\\\"\" + adRightSrc + \"\\\"   width=\\\"\" + adLeftWidth + \"\\\" height=\\\"\" + adRightHeight + \"\\\"  border=\\\"0\\\"  \></a></div>\");
		document.write(\"</div>\");
    }
}
load();
function close_float_right(){
	document.getElementById(\"adRightFloat\").style.display=\"none\";
} 
function close_float_left(){
	document.getElementById(\"adLeftFloat\").style.display=\"none\";
}
function close_ad(d){
	document.getElementById(d).style.display=\"none\";
}
</script>";
		}else{
			if(!eregi("<SCRIPT",$code)){
				$_code=$code;
				$code="<SCRIPT LANGUAGE='JavaScript' src='$wwwurl/?app=ad&act=js&ad_id=$rs[keywords]'></SCRIPT>";
			}else{
				$_code=$code;
			}
		}
		if(($rs[begintime]&&$rs[begintime]>$timestamp)||($rs[endtime]&&$timestamp>$rs[endtime]))
		{
			$code=$_code='';
		}
		//必须使用'号,当作是字符串,不能使用"号,防止用户放$变量
		$show.="\$AD_label['$rs[keywords]']=stripslashes('".addslashes($code)."');\r\n";
		$show.="\$_AD_label['$rs[keywords]']=stripslashes('".addslashes($_code)."');\r\n";
		$code=$_code='';
	}	
	write_file(ROOT_PATH."/data/ad/ad_cache.php",$show);
}
	
}

?>